/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lb;

import general.Agent;
import general.AgentType;
import general.Commands;
import general.Request;
import general.RequestBuilder;
import general.UDPConnection;
import general.UDPMessage;
import general.UDPTestConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
 *
 * @author foliveira
 */
public class UDPLoadBalance extends Agent {

    private Set<String> setServer;
    private Map<String, Integer> mapJobs;
    private UDPTestConnection tConnection;
    private UDPConnection connection;
    private UDPMessage message;
    private RequestBuilder request;
   
    public UDPLoadBalance(String name, int port) {

        this.setPort(port);
        this.setName(name);
        
        initComponents();
        
        displayOutput(String.format("LoadBalance %s %s",this.getName(), this.getPort()));
        whaitForMessage();

    }

    private void whaitForMessage() {

        String command=null;
        Request response;
        
        
        while (true) {

            try {

                
                request = new RequestBuilder(message.receive());
                response = request.getRequest();
                command = response.getCommand();
                
                
                if (command.contains("WHO"))
                {
                    displayOutput("recebeu " + response.toString());
                    response.setCommand("DATASERVICE");                  
                    response.setAgent("LOADBALANCE"); 
                    response.setData("10.10.1.80:8080");
                    message.reply(response.toString());
                    
                } else if(command.contains("ECHO")) {                    
                    displayOutput("recebeu: " + response.toString());
                    response.setAgent("LOADBALANCE");
                    message.reply(response.toString());
                    
                } else if (command.equals("ADD")){
                    displayOutput("recebeu " + response.toString() );
                    
                }


            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }

    }

    public void answerRequest(UDPMessage message, Request request) {
        
        request.setCommand("AVALIABLE");
        request.setData(getMinorPickload());

        try {
            message.reply(request.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }

    private String getMinorPickload() {
        int i;
        String key = null;
        String min = null;
        Integer value;

        i = mapJobs.get("1");

        for (Map.Entry<String, Integer> entry : mapJobs.entrySet()) {

            key = entry.getKey();
            value = entry.getValue();

            if (value < i) {
                min = key;
            }

        }

        return min;

    }

    private void addServerAvaliable(UDPMessage message) {
        String host;
        host = String.format("%s:%s", message.getPacket().getAddress().getHostName(),
                message.getPacket().getPort());

        setServer.add(host);

    }

    private void addJobs(UDPMessage message, Request request) {
        String host;
        host = String.format("%s:%s", message.getPacket().getAddress().getHostName(),
                message.getPacket().getPort());

        // adicona os dados    
        mapJobs.put(host, Integer.getInteger(request.getData()));

    }
    
    private void displayOutput(String log){
        System.out.println(log);
    }

    private void initComponents() {
        
        connection = new UDPConnection(this.getPort());
        setServer = new HashSet<String>();
        mapJobs = new HashMap<String, Integer>();
        message = new UDPMessage(connection);
        
    }
    
    
}
